%% Author: wintereagle   
%% Created: 2007-1-6
%% Description: TODO: Add desciption to aleph_log
-module(aleph_log).
-include("aleph_naming_macros.hrl").
-include("aleph_service_macros.hrl").
-behaviour(gen_server).
-import(aleph_process_labeler,[label_process/1]).
-export([handle_call/3,handle_info/2,handle_cast/2,
         start_link/0,start/0,init/0,init/1,log/3,trace/1,info/1,warning/1,
         error/1,trace/2,info/2,warning/2,error/2]).

%%%%%%%%%%%%%behaviour implementations
start_link() ->
   start_log(start_link).

start()-> 
   start_log(start).

start_log(Start)->     
   case  Ret=label_process(
                  gen_server:Start({local,?LOG_PROCESS},aleph_log, [], [])) of
        {ok,_}->A=init(),io:format("1111~p~n",[A]),Ret;
        _     ->Ret
   end.
%%%%%%%%%%%%%callback implementations
init([]) ->
    process_flag(trap_exit, true),   
    {ok,ok};
init(_)->
    Str=atom_to_list(?LOG),       
    {ok,Log}=disk_log:open([{name,?LOG},{file,Str},{type,wrap},{size,{1024*10,1024}}]),
    {reply,Log,Log}.
    
    
-define(SERVICE_NAME,?LOG_PROCESS).
?gen_handle_call.
?gen_handle_cast.  
?gen_handle_info. 
?define_call(init).

trace(Msg)->
    io:format("2~n"),
    log(trace,Msg).
trace(Format,Args)->
    log(trace,Format,Args).
info(Msg)->
    log(info,Msg).
info(Format,Args)->
    log(info,aleph_utility:flat_format(Format,Args)).
warning(Msg)->
    log(warning,Msg).
warning(Format,Args)->
    log(warning,aleph_utility:flat_format(Format,Args)).
error(Msg)->
    log(error,Msg).
error(Format,Args)->
    log(error,aleph_utility:flat_format(Format,Args)).
log(Level,Format,Args)->
    log(Level,aleph_utility:flat_format(Format,Args)).
log(Level,Msg)->
    io:format("~p,~p~n",[Level,Msg]),
    T=erlang:process_info(self(),registered_name),
    io:format("222222~p~n",[T]),
    {_,Name}=T,
    Date=aleph_utility:date_to_string(now()),
    io:format("~p~n",[Date]),
    Log_Record={Name,Date,Level,Msg},
    io:format("~p~n",[Log_Record]),
    disk_log:alog(?LOG,Log_Record).


